Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Is it possible to abort the http request[GET, POST, etc] in HttpAsynClient from HttpClient-5.x?

I'm using org.apache.hc.client5.http.impl.async.HttpAsyncClients.create() to create org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient for my HTTP/2 requests. I'm looking for some functionality to abort the request after reading some amount of data from the socket channel.

i tried with the cancel(mayInterruptIfRunning) method from the Future instance. But after abort it i can't get the response headers and the downloaded content.

    Future<SimpleHttpResponse> future = null;
    CloseableHttpAsyncClient httpClient = null;
    try {
        httpClient = httpAsyncClientBuilder.build();
        httpClient.start();
        future = httpClient.execute(target, asyncRequestProducer, SimpleResponseConsumer.create(), null, this.httpClientContext, this);
        future.get(10, TimeUnit.SECONDS);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        httpClient.close(CloseMode.GRACEFUL);
    }

Is there any other way to achieve this with httpclient-5.x?

Thanks in advance.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Of course, it is. But you need to implement your own custom response consumer that can return a partial message content

try (CloseableHttpAsyncClient httpClient = HttpAsyncClients.createDefault()) {
    httpClient.start();

    final Future<Void> future = httpClient.execute(
            new BasicRequestProducer(Method.GET, new URI("http://httpbin.org/")),
            new AbstractCharResponseConsumer<Void>() {

                @Override
                protected void start(
                        final HttpResponse response,
                        final ContentType contentType) throws HttpException, IOException {
                    System.out.println(response.getCode());
                }

                @Override
                protected int capacityIncrement() {
                    return Integer.MAX_VALUE;
                }

                @Override
                protected void data(final CharBuffer src, final boolean endOfStream) throws IOException {
                }

                @Override
                protected Void buildResult() throws IOException {
                    return null;
                }

                @Override
                public void releaseResources() {
                }

            }, null, null);
    try {
        future.get(1, TimeUnit.SECONDS);
    } catch (TimeoutException ex) {
        future.cancel(true);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!